package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.dao.*;
import com.yenlien.traceyun.control.entity.Goods;
import com.yenlien.traceyun.control.entity.GoodsLevel;
import com.yenlien.traceyun.control.entity.StoreOrder;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jialong
 * @date 2021/06/01
 * @description:
 **/
@Service
public class StoreOrderService {

    @Autowired
    private StoreOrderDao storeOrderDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private ShelvesDao shelvesDao;

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private GoodsLevelDao goodsLevelDao;


    public Object save(StoreOrder storeOrder) {
        // 出货量不能大于存货量
        Goods goods = goodsDao.findBySId(storeOrder.getShelvesId());
        if (goods == null) {
            throw new BusinessException("该货架没有对应的货物");
        }
        Integer goodsNum = goods.getGoodsNum();
        if (storeOrder.getNums() > goodsNum) {
            throw new BusinessException("交易数量不能大于存货量");
        } else if (storeOrder.getNums() == goodsNum) {
            // 应将货架清空
            goods.setShelvesId(null);
            goodsDao.save(goods);
        } else {
            int surplus = goodsNum - storeOrder.getNums();
            goods.setGoodsNum(surplus);
            goodsDao.save(goods);
        }
        return storeOrderDao.save(storeOrder);
    }


    public Page<StoreOrder> findByList(StoreOrder storeOrder) {
        Pageable pageable = PageRequest.of(storeOrder.getPage(), storeOrder.getPageSize(), Sort.Direction.ASC, ("createTime"));
        Page<StoreOrder> orders = storeOrderDao.findAll((Root<StoreOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.isNotBlank(storeOrder.getName())) {
                list.add(cb.like(root.get("name"), "%" + storeOrder.getName() + "%"));
            }
            if (storeOrder.getPositionId() != null) {
                list.add(cb.equal(root.get("positionId"), storeOrder.getPositionId()));
            }
            String companyId = UserAuthUtil.getLoginUser().getCompanyId();
            list.add(cb.equal(root.get("companyId"), companyId));
            Predicate[] predicate = new Predicate[list.size()];
            return cb.and(list.toArray(predicate));
        }, pageable);
        for (StoreOrder storeOrder1 : orders.getContent()) {
            storeOrder1.setSurplusNum(goodsDao.findBySId(storeOrder1.getShelvesId()).getGoodsNum());
        }
        return orders;
    }

    public Object findById(String id) {
        return storeOrderDao.findById(id);
    }

    public Object getAllPositions(String companyId) {
        return positionDao.findByCompanyId(companyId);
    }

    public Object getShelvesByPosition(Long positionId) {
        return shelvesDao.getShelvesByPosition(positionId);
    }

    public Object getGoodsBySId(Long shelvesId) {
        Goods goods = goodsDao.findBySId(shelvesId);
        if (goods == null) {
            throw new BusinessException("该货架没有对应的货物");
        }
        GoodsLevel goodsLevel = goodsLevelDao.findById(goods.getGoodsLevel()).get();
        if (goodsLevel == null) {
            throw new BusinessException("该品级不存在");
        }
        goods.setGoodsLevelName(goodsLevel.getName());
        return goods;
    }

}
